using System.Collections.Generic;
using DataSystem;
using FairyGUI;
using FairyGUI.Utils;
using Utility;
using Utility.DataSystem;

namespace UIPackage_Main
{
	public partial class UI_View_Game
	{
		private IReadOnlyList<TileGroupSetting> _tileGroupSettings;
		partial void OnCreate ()
		{
			GRoot.inst.AddChild (this);
			MakeFullScreen();
			AddRelation (GRoot.inst, RelationType.Size);
			visible = false;
			fairyBatching = true;
			
			m_GList_HandfulItem.itemRenderer = ItemRenderer;
			Stage.inst.onClick.Add (OnClicked_Stage);
			Stage.inst.onRightClick.Add (OnRightClicked_Stage);
		}

		private void OnClicked_Stage (EventContext context)
		{
			if (visible)
			{
				if (!Stage.isTouchOnUI)
				{
					Main.Game.Construction.Build_TryPlace ();
				}
			}
		}
		private void OnRightClicked_Stage (EventContext context)
		{
			if (visible)
			{
				if (!Stage.isTouchOnUI)
				{
					Main.Game.Construction.TryRemove ();
				}
			}
		}
		
		private void ItemRenderer (int index, GObject item)
		{
			if (item is UI_Item_Handful itemHandful)
			{
				itemHandful.UpdateUI (_tileGroupSettings.TryGet (index));
			}
		}

		public void Show ()
		{
			visible = true;
			_tileGroupSettings = SettingCenter.GetSettings<TileGroupSetting> ();
			m_GList_HandfulItem.numItems = _tileGroupSettings.Count;
		}

		public void Hide ()
		{
			
		}
	}
}